package gui.win.tree;

import gui.Segl1;
import gui.cen.Center;
import gui.utils.Borders;
import gui.utils.Para;
import gui.utils.Slider;
import gui.utils.SliderWrap;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class FullT extends JFrame implements ActionListener{
    private JTextArea label_1;
    private JComboBox codon_0, codon_1, codon_2, codon_3;

    public FullT(Center center, Segl1 Seg_L1){
        super();
        this.setLocation(Seg_L1.getLocationOnScreen().x + 50, Seg_L1.getLocationOnScreen().y + 50);
        this.setLayout(new FlowLayout());
        this.setSize(new Dimension(400, 262));
        JPanel p = new JPanel();
        p.setBorder(Borders.getTitledBorder(Borders.STANDARD_TITLED_BORDER, "CREATE FULL TREE"));
        p.setLayout(new BorderLayout());

        JPanel p2 = new JPanel(new BorderLayout());
        p2.add(new SliderWrap(Slider.TREEWIN, 500, 30, Para.TREEWIN, "Tree Window", 260, 3, this), BorderLayout.NORTH);
        label_1 = new JTextArea();
        label_1.setFont(Borders.getFont(Borders.GUIFONT));
        String txt = "Reads spanning site "+ Para.TPOS+" to "+ (Para.TPOS+Para.TREEWIN)+" will be included\nwithin the tree. ";
        txt = txt +  "\n\nDrop lists below can be used to select up to 4 codons\n" +
                     "for which infromation will be included within the leaf labels.\n" +
                     "These can be searched and coloured individually following tree\ngeneration.";
        label_1.setText(txt);
        p2.add(label_1, BorderLayout.CENTER);
        JPanel p3 = new JPanel(new GridLayout(1,4));
        p3.setBorder(BorderFactory.createEmptyBorder(4,4,4,0));
        p3.add(codon_0 = fillBox());
        p3.add(codon_1 = fillBox());
        p3.add(codon_2 = fillBox());
        p3.add(codon_3 = fillBox());
        p2.add(p3, BorderLayout.SOUTH);

        p.add(p2, BorderLayout.CENTER);


        JPanel p5 = new  JPanel(new BorderLayout());

        p.add(p5, BorderLayout.SOUTH);


        this.add(p, BorderLayout.CENTER);
        this.add(new Buttons(this, center), BorderLayout.SOUTH);
        this.setVisible(true);
        this.setAlwaysOnTop(true);
    }
    public void closeFrame(){this.dispose();}

    public void updateTextAndResetCodons(){
        String txt = "Reads spanning site "+ Para.TPOS+" to "+ (Para.TPOS+Para.TREEWIN)+" will be included\nwithin the tree. ";
        txt = txt +  "\n\nDrop lists below can be used to select up to 4 codons\n" +
                     "for which infromation will be included within the leaf labels.\n" +
                     "These can be searched and coloured individually following tree\ngeneration.";
        label_1.setText(txt);

        codon_0.setSelectedIndex(0);
        Para.TCODS[0] = -1;
        codon_1.setSelectedIndex(0);
        Para.TCODS[1] = -1;
        codon_2.setSelectedIndex(0);
        Para.TCODS[2] = -1;
        codon_3.setSelectedIndex(0);
        Para.TCODS[3] = -1;
    }

    public int [] correspondingSite;
    private JComboBox fillBox(){
        String[] models = new String[(int)Math.ceil(Para.TREEWIN/3)+1];
        correspondingSite = new int[(int)Math.ceil(Para.TREEWIN/3)+1];
        int counter = 0;
        models[counter] = "none";
        correspondingSite[counter++] = -1;
        for(int readSite=0; readSite<Para.TREEWIN-3; readSite++){
            int templateSite = Para.TPOS + readSite;
            if((templateSite - Para.FRAME)%3==0){
                models[counter] = templateSite + "";
                correspondingSite[counter] = templateSite;
                counter++;
            }
        }
        JComboBox mods = new JComboBox(models);
        if(mods!=null){
            mods.addActionListener(this);
            mods.setFont(Borders.getFont(Borders.GUIFONT));
        }
        return mods;
    }

    public void actionPerformed(ActionEvent ae) {
        Object source = ae.getSource();
        if(source == codon_0){Para.TCODS[0] = correspondingSite[codon_0.getSelectedIndex()];}
        else if(source == codon_1){Para.TCODS[1] = correspondingSite[codon_1.getSelectedIndex()];}
        else if(source == codon_2){Para.TCODS[2] = correspondingSite[codon_2.getSelectedIndex()];}
        else if(source == codon_3){Para.TCODS[3] = correspondingSite[codon_3.getSelectedIndex()];}
    }
}